package cn.com.dhc.danlu.widget.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.ArrayList;

import cn.com.dhc.danlu.R;

/**
 * @author Administrator
 */
public class CategorySpinnerListViewAdapter extends BaseAdapter {
    private Context context;
    private ArrayList<String> list;
    private int mDefaultSelectedPos = -1;

    public CategorySpinnerListViewAdapter (Context context, ArrayList<String> list) {
        this.context = context;
        this.list = list;
    }

    public void setDefaultSelectedPos (int mDefaultSelectedPos) {
        this.mDefaultSelectedPos = mDefaultSelectedPos;
    }

    @Override
    public int getCount () {
        return list.size ();
    }

    @Override
    public Object getItem (int arg0) {
        return arg0;
    }

    @Override
    public long getItemId (int arg0) {
        return arg0;
    }

    @Override
    public View getView (int arg0, View arg1, ViewGroup arg2) {
        ViewHolder viewHolder;
        if (arg1 == null && list.size () != 0) {
            viewHolder = new ViewHolder ();
            LayoutInflater inflater = LayoutInflater.from (context);
            arg1 = inflater.inflate (R.layout.layout_spinner_item_txt, null);
            viewHolder.textView = (TextView) arg1.findViewById (R.id.itemText);
            arg1.setTag (viewHolder);
        } else {
            viewHolder = (ViewHolder) arg1.getTag ();
        }
        viewHolder.textView.setText (list.get (arg0));
        if (arg0 == mDefaultSelectedPos) {
            viewHolder.textView.setTextColor (context.getResources ().getColor (R.color.red));
        } else {
            viewHolder.textView.setTextColor (context.getResources ().getColor (R.color.black));
        }
        return arg1;
    }

    public static class ViewHolder {
        public TextView textView;
    }
}